package org.gaaidou.ptarmigan.springboot.functional.controller;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.gaaidou.ptarmigan.springboot.core.async.AsyncCaller;
import org.gaaidou.ptarmigan.springboot.core.exception.Assert;
import org.gaaidou.ptarmigan.springboot.functional.bean.Gender;
import org.gaaidou.ptarmigan.springboot.functional.service.UserService;
import org.gaaidou.ptarmigan.springboot.functional.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.concurrent.CompletionStage;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<UserVo> get(@PathVariable("id") Long id) {
        return ResponseEntity.ok().body(userService.get(id));
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<Void> add(@RequestBody @Valid UserVo user) {
        user.setId(1);
        Assert.isEquals(user.getGender(), Gender.MALE, "gender must be male");
        userService.add(user);
        return ResponseEntity.created(URI.create("/user/1")).build();
    }


    @PutMapping("/{id}")
    public ResponseEntity<UserVo> update(@PathVariable("id") Long id, @RequestBody @Valid UserVo user) {
        return ResponseEntity.accepted().body(user);
    }


    @PatchMapping("/{id}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public ResponseEntity<UserVo> patch(@PathVariable("id") Long id, @RequestBody @Valid UserVo user) {
        return ResponseEntity.accepted().body(user);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public ResponseEntity<Void> health(@PathVariable("id") Long id) {
        return ResponseEntity.noContent().build();
    }
}
